/**
 * Copyright (c) 2006 Amgad S Youssef
 * All Rights Reserved.
 * Licensed under the Eclipse Public License - v 1.0
 * For more information see http://www.eclipse.org/legal/epl-v10.html
 */
package org.eclipsejcde.core.launchers.arguments;

import java.io.File;
import java.util.Iterator;
import java.util.Map;

import org.eclipsejcde.core.JCDECore;

/**
 * @author Amgad S Youssef
 *
 */
public class ArgumentsPreparer {
	public static String prepareCREFArguments(String outputFileLocation, String[] options) {
		StringBuffer arguments = new StringBuffer("");
		if (options != null) {
			for (int i = 0; i < options.length; i++) {
				if (options[i].startsWith("-i ")) {
					arguments.append("");
				} else if (options[i].startsWith("-o ")) {
					arguments.append("");
				} else {
					arguments.append(options[i] + " ");
				}
			}
		}
		if (outputFileLocation != null && !outputFileLocation.equals("")) {
			if (!outputFileLocation.endsWith(".vjc")) {
				outputFileLocation = outputFileLocation + ".vjc";
			}
			File vjcFile = new File(outputFileLocation);
			if (vjcFile.exists()) {
				arguments.append("-i \"" + outputFileLocation + "\" ");
				arguments.append("-o \"" + outputFileLocation + "\" ");
			} else {
				vjcFile.getParentFile().mkdirs();
				arguments.append("-o \"" + outputFileLocation + "\" ");
			}
		}
		return arguments.toString();
	}

	public static String prepareConverterArguments(String root, String outputRoot, String[] exportPath, String packageName, String packageAID, Map appletAIDs, String majorVersion,
			String minorVersion, String[] options) {
		StringBuffer arguments = new StringBuffer();
		arguments.append("-classdir \"" + root + "\" ");
		arguments.append("-d \"" + outputRoot + "\" ");
		if (exportPath == null || exportPath.length == 0) {
			arguments.append("-exportpath \"" + JCDECore.getCurrentJCDKHome() + File.separator + "api_export_files" + File.pathSeparator + outputRoot + "\" ");
		} else {
			StringBuffer exportPathStrBuf = new StringBuffer(JCDECore.getCurrentJCDKHome() + File.separator + "api_export_files");
			for (int i = 0; i < exportPath.length; i++) {
				exportPathStrBuf.append(File.pathSeparator + exportPath[i]);
			}
			arguments.append("-exportpath \"" + exportPathStrBuf.toString() + "\" ");
		}
		if (appletAIDs != null) {
			Iterator iter = appletAIDs.keySet().iterator();
			while (iter.hasNext()) {
				String appletClassName = (String) iter.next();
				String appletAID = (String) appletAIDs.get(appletClassName);
				arguments.append("-applet " + appletAID + " " + appletClassName + " ");
			}
		}
		if (options != null) {
			for (int i = 0; i < options.length; i++) {
				if (options[i].startsWith("-classdir ")) {
					arguments.append("");
				} else if (options[i].startsWith("-d ")) {
					arguments.append("");
				} else if (options[i].startsWith("-exportpath ")) {
					arguments.append("");
				} else if (options[i].startsWith("-applet ")) {
					arguments.append("");
				} else {
					arguments.append(options[i] + " ");
				}
			}
		}
		arguments.append(packageName + " ");
		arguments.append(packageAID + " ");
		arguments.append(majorVersion + "." + minorVersion);
		return arguments.toString();
	}

	public static String prepareConverterArguments(String root, String outputRoot, String packageName, String packageAID, Map appletAIDs, String majorVersion, String minorVersion, String[] options) {
		return prepareConverterArguments(root, outputRoot, null, packageName, packageAID, appletAIDs, majorVersion, minorVersion, options);
	}

	public static String prepareCapgenArguments(String jcaFileLocation, String outputFileLocation, String[] options) {
		StringBuffer arguments = new StringBuffer();
		if (options != null) {
			for (int i = 0; i < options.length; i++) {
				if (options[i].startsWith("-o ")) {
					arguments.append("");
				} else {
					arguments.append(options[i] + " ");
				}
			}
		}
		arguments.append("-o \"" + outputFileLocation + "\" ");
		arguments.append("\"" + jcaFileLocation + "\"");
		return arguments.toString();

	}

	public static String prepareVerifycapArguments(String[] exportFiles, String capFileLocation, String[] options) {
		StringBuffer arguments = new StringBuffer();
		if (options != null) {
			for (int i = 0; i < options.length; i++) {
				arguments.append(options[i] + " ");
			}
		}
		for (int i = 0; i < exportFiles.length; i++) {
			arguments.append("\"" + exportFiles[i] + "\" ");
		}
		arguments.append("\"" + capFileLocation + "\"");
		return arguments.toString();

	}

	public static String prepareVerifyexpArguments(String expFileLocation, String[] options) {
		StringBuffer arguments = new StringBuffer();
		if (options != null) {
			for (int i = 0; i < options.length; i++) {
				arguments.append(options[i] + " ");
			}
		}
		arguments.append("\"" + expFileLocation + "\"");
		return arguments.toString();

	}

	public static String prepareScriptgenArguments(String capFileLocation, String outputFileLocation, String[] options) {
		StringBuffer arguments = new StringBuffer();
		if (options != null) {
			for (int i = 0; i < options.length; i++) {
				if (options[i].startsWith("-o ")) {
					arguments.append("");
				} else {
					arguments.append(options[i] + " ");
				}
			}
		}
		arguments.append("-o \"" + outputFileLocation + "\" ");
		arguments.append("\"" + capFileLocation + "\"");
		return arguments.toString();

	}

	public static String prepareApdutoolArguments(String[] scriptFiles, String[] options) {
		StringBuffer arguments = new StringBuffer();
		if (options != null) {
			for (int i = 0; i < options.length; i++) {
				arguments.append(options[i] + " ");
			}
		}
		for (int i = 0; i < scriptFiles.length; i++) {
			arguments.append("\"" + scriptFiles[i] + "\" ");
		}
		return arguments.toString();

	}

	public static String prepareJCWDEArguments(String configFileLocation, String[] options) {
		StringBuffer arguments = new StringBuffer();
		if (options != null) {
			for (int i = 0; i < options.length; i++) {
				arguments.append(options[i] + " ");
			}
		}
		arguments.append("\"" + configFileLocation + "\"");
		return arguments.toString();

	}

}
